package com.zemo.base.common.exception;


import com.zemo.base.common.enums.ErrorCodeDef;
import com.zemo.base.common.enums.ErrorCodeEnum;
import com.google.common.base.Joiner;

/**
 * 业务异常基类，所有不符合预期的结果、规则不匹配等可预见的问题都可以抛出该异常
 * <p>
 * 如：余额不足就是一个可预期的异常，而接口超时，接口返回未知的失败则为不可预见异常
 *
 * @author zemochen
 * @Date 2018/01/02
 */
public class PredictableException extends RuntimeException {
    protected String code;
    protected String msg;


    public PredictableException(Throwable e) {
        super(e);
    }

    public PredictableException(String message) {
        this.code = ErrorCodeEnum.INTERNAL_FAIL.getCode();
        this.msg = message;
    }


    public PredictableException(String message, Throwable cause) {
        super(message, cause);
        this.msg = message;
        this.code = ErrorCodeEnum.INTERNAL_FAIL.getCode();
    }

    public PredictableException(ErrorCodeDef errorCode, Object... params) {
        this.code = errorCode.getCode();
        this.msg = String.format(errorCode.getMsg(), params);
    }

    public String getCode() {

        return this.code;
    }


    public String getMsg() {
        return msg;
    }


    @Override
    public String getMessage() {
        return String.format("errorCode=%s,errorMsg=%s", code, msg);
    }
}

